package sanshi.contect.udpstarter.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sanshi.contect.udpstarter.service.UDPMsgHandler;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * 从阻塞队列中的拿去消息，并进行后续处理 msg
 * @author sanshi
 */

@Component
public class ConsumerMsgHandler {

    @Autowired
    private UDPMsgHandler handler;

    private final LinkedBlockingQueue<byte[]> recvLinkedBlockingQueue;

    public ConsumerMsgHandler(LinkedBlockingQueue<byte[]> recvLinkedBlockingQueue) {
        this.recvLinkedBlockingQueue = recvLinkedBlockingQueue;
    }

    public void getMsgFromQueue(){

        try {
            while (true) {
                //take阻塞的拿取数据
                byte[] bytes = recvLinkedBlockingQueue.take();
                //后续处理
                 handler.handMsg(bytes);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
